package pl.ptemich.tankwars.states;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import pl.ptemich.tankwars.GraphicConstants;
import pl.ptemich.tankwars.objects.Bug;
import pl.ptemich.tankwars.objects.GameObject;
import pl.ptemich.tankwars.objects.PhysicalObject;
import pl.ptemich.tankwars.tools.StateTools;

public class PlayState extends RegisteredState {

  /**
   * background 
   */
  private Image background;
  
  private List<Bug> bugs;
  private Integer BUGS_COUNT = 5;
  
  private Bug activeBug = null;
  
  private String message = "";
  
  private Boolean randomPossible = Boolean.TRUE;

  

  
  @Override
  public void mouseClicked(int button, int x, int y, int clickCount) {
    if (Input.MOUSE_LEFT_BUTTON == button) {
      for (Bug bug : bugs) {
        if (bug.checkSelection(x, y)) {
          activeBug = bug;
        }
      }    
    } else if (Input.MOUSE_RIGHT_BUTTON == button) {
      if (activeBug != null) {
        //bug.moveTo(x, y);
      }
    }
  }
  
  @Override
  public void init(GameContainer container, StateBasedGame game) throws SlickException {
    GraphicConstants.initResources();
    
    background = new Image("resources/images/background.png");
    
    bugs = new ArrayList<Bug>();
    Bug tmpBug;
    for (int i = 0; i < BUGS_COUNT; i ++) {
      tmpBug = Bug.getInstance();
      tmpBug.setVisible(true);
      bugs.add(tmpBug);
    }
    
     
  }

  @Override
  public void render(GameContainer container, StateBasedGame game, Graphics graphics)
      throws SlickException {
    background.draw();
    if (bugs != null) {
      for (GameObject bug : bugs) {
        bug.draw();
      }
    }
    graphics.drawString(message, 10, 30);
  }

  @Override
  public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
    Input input = container.getInput();

    if (input.isKeyDown(Input.KEY_ESCAPE)) {
      game.enterState(StateTools.getStateInstance(MenuState.class).getID(), 
                      new FadeOutTransition(Color.black), 
                      new FadeInTransition(Color.black));
    }
    
    message = "";
    
    if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
      message += "LEFT BUTTON PRESSED ";
      for (PhysicalObject bug : bugs) {
        bug.checkSelection(input.getAbsoluteMouseX(), input.getAbsoluteMouseY());
      }
    }
    
    if (input.isMouseButtonDown(Input.MOUSE_MIDDLE_BUTTON)) {
      message += "MIDDLE BUTTON PRESSED ";
      if (randomPossible) {
        for (PhysicalObject bug : bugs) {
          bug.randomPosition();
        }
        randomPossible = Boolean.FALSE;
      }
    } else {
      randomPossible = Boolean.TRUE; 
    }
    
    
    if (input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON)) {
      message += "RIGHT BUTTON PRESSED ";
    }
    
    message += input.getAbsoluteMouseX() + "-" + input.getAbsoluteMouseY();
    
    
    if (bugs != null) {
      for (PhysicalObject bug : bugs) {
        bug.update(delta);
      }
    }    
  }


//  @Override
//  public void keyPressed(int key, char c) {    
//    super.keyPressed(key, c);
//    
//    switch (key) {
//      case Input.KEY_UP:
//        break;
//      case Input.KEY_DOWN:
//        break;
//    }
//  } 
  
//  @Override
//  public void mouseClicked(int button, int x, int y, int clickCount) {
//    // TODO Auto-generated method stub
//    super.mouseClicked(button, x, y, clickCount);
//  }
//  
//  @Override
//  public void mouseReleased(int button, int x, int y) {
//    // TODO Auto-generated method stub
//    super.mouseReleased(button, x, y);
//  }
}
